import json
import pickle
import re

import requests


def select_user():


    data = {
        'device_id': "device_ids4",
        'user': "锄禾",
    }
    url = "http://127.0.0.1:5000/select_user"
    # url = "http://103.149.200.228:8081/select_user"
    response = requests.post(url, json=json.dumps(data))

    # 从回复中 中获取并解析 JSON 数据

    if json.loads(response.text) == "None":
        print("None")
    else:
        user_message_list = json.loads(response.text)
        print(user_message_list)
        return user_message_list


def add_user():
    # 获取原来userlist
    user_message_list = select_user()

    if user_message_list == None:
        user_message_list = []

    # 修改user_message_list
    massage = []
    massage.append("user")
    massage.append("想怎么聊")
    user_message_list.append(massage)

    massage = []
    massage.append('assistant')
    massage.append("都可以")
    user_message_list.append(massage)

    data = {
        'device_id': "s2",
        'user': "大山二号",
        'user_message_list': user_message_list,
    }
    # url = "http://127.0.0.1:5000/add_user"
    url = "http://103.149.200.228:8081/add_user"

    response = requests.post(url, json=json.dumps(data))

    print(response)

def get_device_user_dict(user_str):


    data = {
        'user_str': user_str
    }

    # url = "http://127.0.0.1:5000/get_device_user_dict"
    url = "http://103.149.200.228:8081/get_device_user_dict"
    response = requests.post(url, json=json.dumps(data))

    return json.loads(response.text)



if __name__ == "__main__":
   select_user()


   add_user()

   # device_user_dict = get_device_user_dict("清")
   # print(device_user_dict)